package tail.ui.utils;

import java.io.File;
import javax.swing.JFileChooser;
import tail.utils.Properties;
import tail.utils.Property;

/**
 *
 * @author zmn
 */
public final class FileChooser {

    /**
     * Private default constructor.
     */
    private FileChooser() { }

    /**
     * Static JFileChooser used for singleton access.
     */
    private static JFileChooser staticFileChooser = null;

    /**
     * Singleton implementation for the file chooser.
     * @return Instance of JFileChooser
     */
    private static synchronized JFileChooser instance() {
        if (staticFileChooser == null) {
            staticFileChooser = new JFileChooser();
            staticFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        }
        return staticFileChooser;
    }

    /**
     * Shows an open dialog.
     * @return List of selected files
     */
    public static File[] showOpen() {
        File[] files = null;
        instance().setMultiSelectionEnabled(true);
        File initDir = new File(Properties.get(Property.INITIAL_DIRECTORY));
        if (initDir.exists()) {
            staticFileChooser.setCurrentDirectory(initDir);
        }

        if (instance().showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            Properties.set(Property.INITIAL_DIRECTORY,
                    staticFileChooser.getCurrentDirectory().getPath());
            Properties.save();
            files = instance().getSelectedFiles();
        }
        return files;
    }
}
